package me.jessyan.mvparms.demo.mvp.ui.activity.shoppcard;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DoubleClickUtils;
import com.jess.arms.widget.CustomPopupWindow;

import javax.inject.Inject;
import javax.inject.Named;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.di.component.shoppcard.DaggerCardDetailComponent;
import me.jessyan.mvparms.demo.di.module.shoppcard.CardDetailModule;
import me.jessyan.mvparms.demo.mvp.contract.shoppcard.CardDetailContract;
import me.jessyan.mvparms.demo.mvp.presenter.shoppcard.CardDetailPresenter;
import me.jessyan.mvparms.demo.mvp.ui.widget.CustomToolbar;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * 失效卡
 * 购物卡
 * 绑定卡 详情
 */
public class CardDetailActivity extends BaseActivity<CardDetailPresenter> implements CardDetailContract.View {
    // 购物卡
    public static final int CARD_SHOPPING = 1;
    //失效卡
    public static final int CARD_INVAILD = 2;
    //绑定卡
    public static final int CARD_BIND = 3;

    int type;
    @BindView(R.id.toolbar)
    CustomToolbar toolbar;
    @BindView(R.id.qr_code_iv)
    ImageView qrCodeIv;
    @BindView(R.id.bottom_tv)
    TextView bottomTv;
    @BindView(R.id.invalid)
    TextView invalidNotice;

    @Inject
    @Named("unbindPop")
    CustomPopupWindow unbindPop;
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        type = getIntent().getIntExtra("type", 0);
        DaggerCardDetailComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .cardDetailModule(new CardDetailModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_card_detail; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        if (type == CARD_SHOPPING) {//只有购物卡才能解绑
            toolbar.setRightIv(R.drawable.ic_toobar_gray_point);

        }

        toolbar.setListener(new CustomToolbar.ToolbarListener() {
            @Override
            public void onLeftBack() {
                killMyself();
            }

            @Override
            public void onRightBack() {
                if(unbindPop!=null){
                    //这个东西再7.0 以后会有问题
                    if (Build.VERSION.SDK_INT >= 24) {

                        unbindPop.measureHeight();
                        unbindPop.showAsDropDown(toolbar.getRightIv(), -ArmsUtils.dip2px(getApplication(), 74), 0, Gravity.LEFT);
                    } else {

                        unbindPop.showAsDropDown(toolbar.getRightIv(), -ArmsUtils.dip2px(getApplication(), 74), 0, Gravity.LEFT);
                    }

                }
                }

        });

        unbindPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1);
            }
        });

        initV();
    }

    private void initV() {
    switch (type){
        //购物卡
        case CARD_SHOPPING:
            setUIShopping();
            break;
            //失效卡
        case CARD_INVAILD:
            setUIInvaild();
            break;
            //绑定卡
        case CARD_BIND:
            setUIBind();
            break;
    }
    }


    private void setUIBind() {
        bottomTv.setVisibility(View.VISIBLE);
        bottomTv.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),R.color.color_F66440));
        bottomTv.setTextColor(ContextCompat.getColor(getApplicationContext(),R.color.white));
        bottomTv.setText("立即绑定");
    }

    /**
     * 失效卡详情ui
     */
    private void setUIInvaild() {
        bottomTv.setVisibility(View.VISIBLE);
        bottomTv.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),R.color.color_E7E9F3));
        bottomTv.setTextColor(ContextCompat.getColor(getApplicationContext(),R.color.color_ff_7A7E95));
        bottomTv.setText("解除绑定");
        invalidNotice.setVisibility(View.VISIBLE);
    }

    /**
     * 购物卡详情ui
     */
    private void setUIShopping() {
        bottomTv.setVisibility(View.GONE);

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }



    @OnClick({R.id.consumption_record_tv, R.id.apply_stores_tv, R.id.bottom_tv})
    public void onViewClicked(View view) {
        if(DoubleClickUtils.getInstance().isDouble(view)){
            return;
        }
        switch (view.getId()) {
            case R.id.consumption_record_tv:
                    startActivity(new Intent(this,ConsumptionRecordActivity.class));
                break;
            case R.id.apply_stores_tv:
                startActivity(new Intent(this,ApplyStoreActivity.class));
                break;
            case R.id.bottom_tv:
                break;
        }
    }

    @Override
    public CardDetailActivity getActivity() {
        return this;
    }

    @Override
    public void dismissPop() {
            if(unbindPop!=null&&unbindPop.isShowing()){
                unbindPop.dismiss();
            }
    }

    @Override
    public void popBackUnbind() {
            finish();
    }
}
